package com.scheduling.inventory.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.scheduling.common.result.Result;
import com.scheduling.inventory.dto.MaterialInventoryDTO;
import com.scheduling.inventory.dto.MaterialInventoryQueryDTO;
import com.scheduling.inventory.service.MaterialInventoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 原料库存Controller
 *
 * @author 开发团队
 * @since 2025-05-29
 */
@Slf4j
@Tag(name = "原料库存管理", description = "原料库存的增删改查、库存调整、库存预警等功能")
@RestController
@RequestMapping("/api/inventory/material")
@RequiredArgsConstructor
@Validated
public class MaterialInventoryController {

    private final MaterialInventoryService materialInventoryService;

    @Operation(summary = "分页查询原料库存", description = "支持按原料名称、库存数量等条件查询")
    @PostMapping("/page")
    public Result<IPage<MaterialInventoryDTO>> getInventoryPage(@RequestBody MaterialInventoryQueryDTO queryDTO) {
        log.info("分页查询原料库存，参数：{}", queryDTO);
        IPage<MaterialInventoryDTO> page = materialInventoryService.getInventoryPage(queryDTO);
        return Result.success(page);
    }

    @Operation(summary = "根据库存ID查询库存详情")
    @GetMapping("/{inventoryId}")
    public Result<MaterialInventoryDTO> getInventoryById(
            @Parameter(description = "库存ID") @PathVariable String inventoryId) {
        log.info("查询库存详情，库存ID：{}", inventoryId);
        MaterialInventoryDTO inventory = materialInventoryService.getInventoryById(inventoryId);
        return Result.success(inventory);
    }

    @Operation(summary = "根据原料ID查询库存信息")
    @GetMapping("/material/{materialId}")
    public Result<MaterialInventoryDTO> getInventoryByMaterialId(
            @Parameter(description = "原料ID") @PathVariable String materialId) {
        log.info("根据原料ID查询库存，原料ID：{}", materialId);
        MaterialInventoryDTO inventory = materialInventoryService.getInventoryByMaterialId(materialId);
        return Result.success(inventory);
    }

    @Operation(summary = "新增原料库存")
    @PostMapping
    public Result<Boolean> createInventory(@Validated @RequestBody MaterialInventoryDTO inventoryDTO) {
        log.info("新增原料库存，参数：{}", inventoryDTO);
        boolean result = materialInventoryService.createInventory(inventoryDTO);
        return Result.success(result, "库存新增成功");
    }

    @Operation(summary = "更新原料库存")
    @PutMapping
    public Result<Boolean> updateInventory(@Validated @RequestBody MaterialInventoryDTO inventoryDTO) {
        log.info("更新原料库存，参数：{}", inventoryDTO);
        boolean result = materialInventoryService.updateInventory(inventoryDTO);
        return Result.success(result, "库存更新成功");
    }

    @Operation(summary = "删除原料库存")
    @DeleteMapping("/{inventoryId}")
    public Result<Boolean> deleteInventory(
            @Parameter(description = "库存ID") @PathVariable String inventoryId) {
        log.info("删除原料库存，库存ID：{}", inventoryId);
        boolean result = materialInventoryService.deleteInventory(inventoryId);
        return Result.success(result, "库存删除成功");
    }

    @Operation(summary = "批量删除原料库存")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDeleteInventory(@RequestBody List<String> inventoryIds) {
        log.info("批量删除原料库存，数量：{}", inventoryIds.size());
        boolean result = materialInventoryService.batchDeleteInventory(inventoryIds);
        return Result.success(result, "批量删除成功");
    }

    @Operation(summary = "调整库存数量", description = "正数为增加库存，负数为减少库存")
    @PutMapping("/adjust/{materialId}")
    public Result<Boolean> adjustInventoryQuantity(
            @Parameter(description = "原料ID") @PathVariable String materialId,
            @Parameter(description = "调整数量") @RequestParam BigDecimal quantity,
            @Parameter(description = "调整原因") @RequestParam String reason) {
        log.info("调整库存数量，原料ID：{}，数量：{}，原因：{}", materialId, quantity, reason);
        boolean result = materialInventoryService.adjustInventoryQuantity(materialId, quantity, reason);
        return Result.success(result, "库存调整成功");
    }

    @Operation(summary = "查询库存不足的原料列表")
    @GetMapping("/low-stock")
    public Result<List<MaterialInventoryDTO>> getLowStockMaterials() {
        log.info("查询库存不足的原料列表");
        List<MaterialInventoryDTO> lowStockList = materialInventoryService.getLowStockMaterials();
        return Result.success(lowStockList);
    }

    @Operation(summary = "检查库存是否充足")
    @GetMapping("/check-sufficient/{materialId}")
    public Result<Boolean> checkInventorySufficient(
            @Parameter(description = "原料ID") @PathVariable String materialId,
            @Parameter(description = "所需数量") @RequestParam BigDecimal requiredQuantity) {
        log.info("检查库存充足性，原料ID：{}，所需数量：{}", materialId, requiredQuantity);
        boolean sufficient = materialInventoryService.checkInventorySufficient(materialId, requiredQuantity);
        return Result.success(sufficient, sufficient ? "库存充足" : "库存不足");
    }

    @Operation(summary = "消费库存", description = "生产时消费原料库存")
    @PutMapping("/consume/{materialId}")
    public Result<Boolean> consumeInventory(
            @Parameter(description = "原料ID") @PathVariable String materialId,
            @Parameter(description = "消费数量") @RequestParam BigDecimal quantity,
            @Parameter(description = "消费原因") @RequestParam String reason) {
        log.info("消费库存，原料ID：{}，数量：{}，原因：{}", materialId, quantity, reason);
        boolean result = materialInventoryService.consumeInventory(materialId, quantity, reason);
        return Result.success(result, "库存消费成功");
    }

    @Operation(summary = "补充库存", description = "采购入库时补充原料库存")
    @PutMapping("/replenish/{materialId}")
    public Result<Boolean> replenishInventory(
            @Parameter(description = "原料ID") @PathVariable String materialId,
            @Parameter(description = "补充数量") @RequestParam BigDecimal quantity,
            @Parameter(description = "补充原因") @RequestParam String reason) {
        log.info("补充库存，原料ID：{}，数量：{}，原因：{}", materialId, quantity, reason);
        boolean result = materialInventoryService.replenishInventory(materialId, quantity, reason);
        return Result.success(result, "库存补充成功");
    }
} 